package com.lv.controller;

import cn.hutool.json.JSONUtil;
import com.lv.data.Result;
import com.lv.pojo.*;
import com.lv.service.CatService;
import com.lv.service.GoodsService;
import com.lv.service.TypeService;
import com.lv.vo.TemplateVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @DubboReference
    private GoodsService gs;
    @DubboReference
    private CatService cs;
    @DubboReference
    private TypeService ts;

    @RequestMapping("togoods")
    public String togoods() {
        return "admin/goods_edit";
    }

    @RequestMapping("getOne")
    @ResponseBody
    public Result getOne(Long parentId) {
        List<TbItemCat> list = cs.getOne(parentId);
        return Result.OK().setData(list);
    }
    @RequestMapping("getCat")
    @ResponseBody
    public Result getCat(Long parentId) {
        Long id = parentId;
        if (id.equals(0) && id == 0){
            return Result.OK().setData(0);
        }
        TbItemCat c = cs.selectID(id);
        return Result.OK().setData(c);
    }
    @RequestMapping("getImt")
    @ResponseBody
    public Result getImt(Long id) {
        try {
            TbItemCat c = cs.selectID(id);
            Long typeId = c.getTypeId();
            TbTypeTemplate type = ts.selectById(typeId);
            return Result.OK().setData(type);
        } catch (Exception e) {
            System.out.println(id);
        }
        return null;

    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(TbGoods goods, TbGoodsDesc dase,String items,String title) {
        List<TbItem> parse = JSONUtil.toList(items, TbItem.class);
        List<TbItem>  name = JSONUtil.toList(title, TbItem.class);


        Subject subject = SecurityUtils.getSubject();
        TbSeller seller = (TbSeller) subject.getPrincipal();
        goods.setSellerId(seller.getSellerId());
        Integer i = gs.addGoodsAndDese(goods,dase,parse,name);
        return Result.OK();
    }

    //根据分类的三级联动id查询模板数据
    @RequestMapping("getType")
    @ResponseBody
    public Result getType(Long itemCatId){
        TemplateVO type = ts.getType(itemCatId);
        return Result.OK().setData(type);
    }


}
